package com.youkeyi.ddy.cloud.application.system.dictionary.controller;

import com.youkeyi.ddy.cloud.common.api.application.system.dictionary.SystemDictionaryCloudService;
import com.youkeyi.ddy.cloud.common.api.application.system.dictionary.dto.SystemDictionaryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.dictionary.dto.SystemDictionaryListItemCloudDto;
import com.youkeyi.ddy.cloud.common.api.application.system.dictionary.param.SystemDictionaryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.dictionary.param.SystemDictionaryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.dictionary.param.SystemDictionaryListCloudParam;
import com.youkeyi.ddy.cloud.common.api.application.system.dictionary.param.SystemDictionaryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.PublicDictionaryCloudService;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.dto.PublicDictionaryListCloudDto;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.param.PublicDictionaryCreateCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.param.PublicDictionaryDeleteCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.param.PublicDictionaryQueryCloudParam;
import com.youkeyi.ddy.cloud.common.api.basic.pub.dictionary.param.PublicDictionaryUpdateCloudParam;
import com.youkeyi.ddy.cloud.common.context.YoukeyiSystemContext;
import com.youkeyi.ddy.cloud.common.enums.ResponseCodeEnum;
import com.youkeyi.ddy.cloud.common.response.ResponseContent;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * SystemDictionaryController
 *
 * @Author liuxiawang
 * @Date 2024/3/21 3:17 PM
 * @Location shenzhen.china
 */
@Service
public class SystemDictionaryController implements SystemDictionaryCloudService {

    @Autowired
    private PublicDictionaryCloudService publicDictionaryCloudService;

    @Override
    public SystemDictionaryListCloudDto list(SystemDictionaryListCloudParam param) {
        SystemDictionaryListCloudDto dto = new SystemDictionaryListCloudDto();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            dto.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return dto;
        }
        if (null == param) {
            dto.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return dto;
        }

        PublicDictionaryQueryCloudParam serverQueryCloudParam = new PublicDictionaryQueryCloudParam();
        serverQueryCloudParam.setDictionaryPrimaryKeys(param.getDictionaryPrimaryKeys());
        serverQueryCloudParam.setCorpKeys(Collections.singletonList(corpKey));
        serverQueryCloudParam.setDictTypes(param.getDictTypes());
        serverQueryCloudParam.setDictKeys(param.getDictKeys());
        serverQueryCloudParam.setPageSize(param.getPageSize());
        serverQueryCloudParam.setCurrentPage(param.getCurrentPage());
        PublicDictionaryListCloudDto serverDtos = publicDictionaryCloudService.query(serverQueryCloudParam);
        if (null != serverDtos && CollectionUtils.isNotEmpty(serverDtos.getRows())) {
            List<SystemDictionaryListItemCloudDto> items = new ArrayList<>();
            serverDtos.getRows().forEach(server -> {
                SystemDictionaryListItemCloudDto item = new SystemDictionaryListItemCloudDto();
                item.setDictionaryPrimaryKey(server.getDictionaryPrimaryKey());
                item.setDictType(server.getDictType());
                item.setDictTypeName(server.getDictTypeName());
                item.setDictKey(server.getDictKey());
                item.setDictKeyName(server.getDictKeyName());
                item.setDictValue(server.getDictValue());
                item.setSortNo(server.getSortNo());
                item.setIconUrl(server.getIconUrl());
                item.setDictDesc(server.getDictDesc());
                item.setIsDeleted(server.getIsDeleted());
                item.setIsDeletedName(server.getIsDeletedName());
                item.setCreateUserKey(server.getCreateUserKey());
                item.setCreateTime(server.getCreateTime());
                item.setUpdateUserKey(server.getUpdateUserKey());
                item.setUpdateTime(server.getUpdateTime());
                item.setCreateUserName(server.getCreateUserName());
                item.setUpdateUserName(server.getUpdateUserName());
                items.add(item);
            });
            dto.setRows(items);
            dto.setTotal(serverDtos.getTotal());
        }
        dto.setSuccessMessage(ResponseCodeEnum.SUCCESS.getMessage());
        return dto;
    }

    @Override
    public ResponseContent create(SystemDictionaryCreateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getDictType())
                || StringUtils.isBlank(param.getDictKey())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicDictionaryCreateCloudParam serverCreateCloudParam = new PublicDictionaryCreateCloudParam();
        serverCreateCloudParam.setCorpKey(corpKey);
        serverCreateCloudParam.setDictType(param.getDictType());
        serverCreateCloudParam.setDictTypeName(param.getDictTypeName());
        serverCreateCloudParam.setDictKey(param.getDictKey());
        serverCreateCloudParam.setDictKeyName(param.getDictKeyName());
        serverCreateCloudParam.setDictValue(param.getDictValue());
        serverCreateCloudParam.setSortNo(param.getSortNo());
        serverCreateCloudParam.setIconUrl(param.getIconUrl());
        serverCreateCloudParam.setDictDesc(param.getDictDesc());
        serverCreateCloudParam.setCreateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        serverCreateCloudParam.setCreateUserName(YoukeyiSystemContext.getUserName());
        return publicDictionaryCloudService.create(serverCreateCloudParam);
    }

    @Override
    public ResponseContent update(SystemDictionaryUpdateCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param
                || StringUtils.isBlank(param.getDictionaryPrimaryKey())
                || StringUtils.isBlank(param.getDictType())
        ) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicDictionaryUpdateCloudParam serverUpdateCloudParam = new PublicDictionaryUpdateCloudParam();
        serverUpdateCloudParam.setDictionaryPrimaryKey(param.getDictionaryPrimaryKey());
        serverUpdateCloudParam.setDictType(param.getDictType());
        serverUpdateCloudParam.setDictTypeName(param.getDictTypeName());
        serverUpdateCloudParam.setDictKey(param.getDictKey());
        serverUpdateCloudParam.setDictKeyName(param.getDictKeyName());
        serverUpdateCloudParam.setDictValue(param.getDictValue());
        serverUpdateCloudParam.setSortNo(param.getSortNo());
        serverUpdateCloudParam.setIconUrl(param.getIconUrl());
        serverUpdateCloudParam.setDictDesc(param.getDictDesc());
        serverUpdateCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        serverUpdateCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicDictionaryCloudService.update(serverUpdateCloudParam);
    }

    @Override
    public ResponseContent delete(SystemDictionaryDeleteCloudParam param) {
        ResponseContent rc = new ResponseContent();
        String corpKey = YoukeyiSystemContext.getCorpPrimaryKey();
        if (StringUtils.isBlank(corpKey)) {
            rc.setErrorMessage(ResponseCodeEnum.TOKEN_INVALID.getMessage());
            return rc;
        }
        if (null == param || CollectionUtils.isEmpty(param.getDictionaryPrimaryKeys())) {
            rc.setErrorMessage(ResponseCodeEnum.PARAMETER_ILLEGAL.getMessage());
            return rc;
        }

        PublicDictionaryDeleteCloudParam serverDeleteCloudParam = new PublicDictionaryDeleteCloudParam();
        serverDeleteCloudParam.setDictionaryPrimaryKeys(param.getDictionaryPrimaryKeys());
        serverDeleteCloudParam.setUpdateUserKey(YoukeyiSystemContext.getUserPrimaryKey());
        serverDeleteCloudParam.setUpdateUserName(YoukeyiSystemContext.getUserName());
        return publicDictionaryCloudService.delete(serverDeleteCloudParam);
    }
}
